package com.htfyun.factorytest.config;

import android.content.Context;
import android.content.SharedPreferences;

public class OTATestConfig {

    private final static String OTA_SHARED_PREF_NAME = "ota_test_shared_pref_name";

    //这个只是为了观看而已, 真正的 result 会写到 nvrom 中
    private static final String OTA_IS_PASSED = "ota_test_pass_boolean";

    private static final String OTA_STARTED = "ota_test_started";
    private static final String OTA_ESSENTIAL = "ota_test_is_essential";

    public static boolean isEssentialForOTATest(Context context) {
        return getBoolean(context, OTA_ESSENTIAL, false);
    }

    public static void setEssentialForOTATest(Context context, boolean essential) {

        setBoolean(context, OTA_ESSENTIAL, essential);

    }

    public static boolean isOTAStarted(Context context) {
        return getBoolean(context, OTA_STARTED, false);
    }

    public static void setOTAStarted(Context context, boolean started) {

        setBoolean(context, OTA_STARTED, started);

    }

    public static boolean isOTAPassed(Context context) {
        return getBoolean(context, OTA_IS_PASSED, false);
    }

    public static void setOTAPassed(Context context, boolean passed) {

        setBoolean(context, OTA_IS_PASSED, passed);

    }

    ///////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////
    private static boolean getBoolean(Context context, String name, boolean defValue) {
        if (context == null) {
            return false;
        }

        SharedPreferences sp = context.getSharedPreferences(OTA_SHARED_PREF_NAME, Context.MODE_PRIVATE);

        return sp.getBoolean(name, defValue);
    }

    private static void setBoolean(Context context, String name, boolean value) {
        if (context == null) {
            return;
        }

        SharedPreferences sp = context.getSharedPreferences(OTA_SHARED_PREF_NAME, Context.MODE_PRIVATE);

        SharedPreferences.Editor editor = sp.edit();

        editor.putBoolean(name, value);

        editor.apply();
    }

}
